var gulp = require('gulp'),  
    sass = require('gulp-ruby-sass'),
    autoprefixer = require('gulp-autoprefixer'),
    minifycss = require('gulp-minify-css'),
    jshint = require('gulp-jshint'),
    uglify = require('gulp-uglify'),
    imagemin = require('gulp-imagemin'),
    rename = require('gulp-rename'),
    clean = require('gulp-clean'),
    concat = require('gulp-concat'),
    notify = require('gulp-notify'),
	changed = require('gulp-changed'),
    cache = require('gulp-cache'),
    livereload = require('gulp-livereload'),
	sourcemaps = require('gulp-sourcemaps');
    fs=require('fs'),
    path=require('path')
	connect = require('connect'),
	livereload = require('gulp-livereload'),
	serveStatic = require('serve-static'),
	serveIndex = require('serve-index');
var slash = require('slash');
var config={
    base:'public'
}

gulp.task('scss', function () {
		return sass('app/src/styles', {sourcemap: true})
        .on('error', function (err) {
            console.error('Error!', err.message);
        })
		.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
		//.pipe(changed('app/dist/styles'))
        .pipe(minifycss())
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('app/dist/styles'));
});

gulp.task('css',function(){
	return gulp.src('app/src/styles/**/*.css')
		.pipe(sourcemaps.init())
		.pipe(changed('app/dist/styles'))
		.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
        .pipe(minifycss())
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('app/dist/styles'));
})

gulp.task('js',function(){
	return gulp.src('app/src/scripts/**/*.js')
	.pipe(sourcemaps.init())
	.pipe(changed('app/dist/scripts'))
//	.pipe(jshint('.jshintrc'))
//  .pipe(jshint.reporter('default'))
    .pipe(uglify())
	.pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('app/dist/scripts'));
});
gulp.task('img',function(){
	return gulp.src(['app/src/images/**/*.jpg','app/src/images/**/*.png','app/src/images/**/*.ico'])
	.pipe(changed('app/dist/images'))
	.pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: false })))
	.pipe(gulp.dest('app/dist/images'));
});


gulp.task('clean',function(){
    return gulp.src('app/dist/')
    .pipe(clean())
})

gulp.task('connect',['scss'],function(){
	var app=connect()
			.use(require('connect-livereload')())
			.use(serveStatic('app'))
			.use(serveIndex('app'))
		require('http').createServer(app)
			.listen(9000)
			.on('listening',function(){
			console.log('Started connect web server on http://localhost:9000');
		})

})

gulp.task('server',['watch'],function(){
	require('opn')('http://localhost:9000');
})

gulp.task('watch',['connect'],function(){
	livereload.listen();
	gulp.watch(['app/src/styles/**/*.scss'],['scss']);
	gulp.watch(['app/src/styles/**/*.css'],['css']);
	gulp.watch(['app/src/scripts/**/*.js'],['js']);
	gulp.watch(['app/src/images/**/*.jpg','app/src/images/**/*.png','app/src/images/**/*.ico'],['img']);
	gulp.watch(['app/dist/styles/**/*.css','app/dist/scripts/**/*.js','app/dist/images/**/*.jpg','app/page/**/*.html']).on('change',livereload.changed);
	
})

gulp.task('build',['clean'],function(){
    gulp.start('scss','js','img','css');

})
gulp.task('start',['scss','js','img','css'],function(){
	gulp.start('server');
})



















